$(function () {
    $("#jqGrid").Grid({
        url: '../toolscheckticket/list',
        colModel: [
            {label: '生产线', index: 'DEV_ID',
                formatter: function (value, col, row) {
                    return row.devProductCode + ' ' + row.devProductName;
                }
            },
            {label: '使用工序', index: 'DEV_ID',
                formatter: function (value, col, row) {
                    return row.devProceNo + ' ' + row.devProceName;
                }
            },
            {label: '设备名称', name: 'devName', index: 'DEV_ID'},
            {label: '设备编号', name: 'devCode', index: 'DEV_ID'},
            {label: '开始日期', name: 'startDate', index: 'START_DATE', width: 90,
                formatter: function (value) {
                    return transDate(value, "yyyy-MM-dd");
                }
            },
            {label: '结束日期', name: 'endDate', index: 'END_DATE', width: 90,
                formatter: function (value) {
                    return transDate(value, "yyyy-MM-dd");
                }
            },
            {label: '实施人', name: 'executorName', index: 'EXECUTOR', width: 90},
            {label: '工单状态', index: 'STATE', width: 80,
                formatter: function (value, col, row) {
                    return sysEnums.TOOLS_CHECK_TICKET_STATUS[row.state];
                }
            },
            {label: '不合格项待处理', name: 'isTreated', index: 'IS_TREATED', width: 120,
                formatter: function (value, col, row) {
                    return value == '1' ? '是' : '否';
                }
            },
            {
                label: '详情', width: 50, sortable: false,
                formatter: function (value, col, row) {
                    return "<button class='btn btn-outline btn-info' id='recordsText' onclick='vm.gdDetail(\"" + row.id + "\",\"" + row.state + "\")'>&nbsp;<i class='fa fa-qrcode'></i></button>";
                }
            },
            {name: 'state', hidden: true},
            {name: 'isTreated', hidden: true},
            {label: '设备ID', name: 'devId', hidden: true},
            {label: '工装检测计划ID', name: 'planId', hidden: true},
            {label: 'id', name: 'id', key: true, hidden: true},
        ],
        gridComplete: function () {
            var ids = $('#jqGrid').getDataIDs();
            for (var i = 0; i < ids.length; i++) {
                var rowData = $('#jqGrid').getRowData(ids[i]);
                if (rowData.isTreated == "1") {
                    $('#' + ids[i]).find("td").addClass("urgeCss");
                }
            }
        }
    });
    vm.getTreeList();
    vm.getStore();
});

//部门树
var deptSetting = {
    data: {
        simpleData: {
            enable: true,
            idKey: "deptId",
            pIdKey: "parentId",
            rootPId: -1
        },
        key: {
            url: "nourl",
            name: "complexName"
        }
    },
    callback: {
        beforeCheck: true,
        beforeClick: outTreeCheck
    }
};

function outTreeCheck(e, treeObj, treeNode) {
    vm.q.deptCode = treeObj.deptId;
    vm.query();
}

let vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        ticketHandleStatusList: enumsToList("TOOLS_CHECK_TICKET_HANDLE_STATUS"),
        toolsCheckTicket: {
            remark: '',
            gmtModified: '',
            gmtCreate: '',
            state: '',
            endDate: '',
            startDate: '',
            devId: '',
            planId: '',
            id: '',
        },
        ruleValidate: {
            name: [
                {required: true, message: '名称不能为空', trigger: 'blur'}
            ]
        },
        q: {
            devName: '',
            deptCode: '', // 部门编码
            isTreated: '',// 是否包含未处理不合格项
            checkMonth: '' // 工单执行月份
        }
    },
    methods: {
        // 查看工单详情
        gdDetail: function (ticketId,state) {
            openWindow({
                type: 2,
                area:['100%','100%'],
                title: '<i class="fa fa-pencil"></i> 检测工单详情',
                content: '../tools/toolscheckticketitem.html?ticketId=' + ticketId + '&state=' + state,
                cancel: function(){
                    // 右上角关闭事件的逻辑
                    vm.reload(true);
                }
            })
        },
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.toolsCheckTicket = {
                remark: '',
                gmtModified: '',
                gmtCreate: '',
                state: '',
                endDate: '',
                startDate: '',
                devId: '',
                planId: '',
                id: '',
            };
        },
        update: function (event) {
            let id = getSelectedRow("#jqGrid");
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id);
        },
        saveOrUpdate: function (event) {
            let url = vm.toolsCheckTicket.id == (null || '') ? "../toolscheckticket/save" : "../toolscheckticket/update";
            Ajax.request({
                url: url,
                params: JSON.stringify(vm.toolsCheckTicket),
                type: "POST",
                contentType: "application/json",
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
            });
        },
        del: function (event) {
            let ids = getSelectedRows("#jqGrid");
            if (ids == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                Ajax.request({
                    url: "../toolscheckticket/delete",
                    params: JSON.stringify(ids),
                    type: "POST",
                    contentType: "application/json",
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    }
                });
            });
        },
        getInfo: function (id) {
            Ajax.request({
                url: "../toolscheckticket/info/" + id,
                async: true,
                successCallback: function (r) {
                    vm.toolsCheckTicket = r.toolsCheckTicket;
                }
            });
        },
        reload: function (event) {
            // 清空postData缓存数据
            var postData = $("#jqGrid").jqGrid("getGridParam", "postData");
            $.each(postData, function (k, v) {
                delete postData[k];
            });
            var queryParam = {};
            if(vm.q.devName){
                queryParam.devName = vm.q.devName;
            }
            if(vm.q.deptCode){
                queryParam.deptCode = vm.q.deptCode;
            }
            if(vm.q.isTreated){
                queryParam.isTreated = vm.q.isTreated;
            }
            if(vm.q.checkMonth){
                queryParam.checkMonth =  gmt2MonthStr(vm.q.checkMonth);
            }
            vm.showList = true;
            let page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: queryParam,
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
        },
        getTreeList: function () {
            Ajax.request({
                url: '../sys/user/treeDeptBySCX',
                async: false,
                successCallback: function (r) {
                    vm.treeList = r.list;
                }
            });
        },
        //加载列表页树
        getStore: function () {
            deptZtree = $.fn.zTree.init($("#storeTree"), deptSetting, vm.treeList);
            // 默认展开工厂一级
            deptZtree.expandNode(deptZtree.getNodeByParam("deptId",'01',null));
        },
        reloadSearch: function () {
            vm.q = {
                devName: '',
                deptCode: '',
                isTreated: '',
                checkMonth: ''
            };
            // 取消选中
            deptZtree.cancelSelectedNode();
            vm.reload();
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        }
    }
});
